//
//  SelectSchoolViewController.m
//  Supervisor
//
//  Created by 小宇 on 2017/5/15.
//  Copyright © 2017年 北京怀教网络科技股份有限公司. All rights reserved.
//

#import "SelectViewController.h"
#import "SelectSchoolTableViewCell.h"

@interface SelectViewController ()
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *responseArr;
@end

@implementation SelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([NSString isEmpty:self.parameterDic[@"themeId"]] && [self.parameterDic.allKeys containsObject:@"themeId"]) {
        [self showMessage:@"请选择督导主题，然后选择督导学校"];
    }
    [self setNavRightBtn];
}

- (void)setNavRightBtn{
    if (self.LzySelectType == LZYSelectMultipleChoice) {
        UIButton *right_button = [self setRightButton];
        [right_button addTarget:self action:@selector(right_click) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:right_button];
        self.navigationItem.rightBarButtonItem = item;
    }
}

- (UIButton *)setRightButton{
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 28, 20.5)];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = AdaptedFontSize(12);
    return button;
}

- (void)right_click{
    self.LzySelectMultiple([NSString arrayToNetWorkString:self.selectedNameList],[NSString arrayToNetWorkString:self.selectedIdList]);
    [self.navigationController popViewControllerAnimated:YES];
}

- (UITableView *)set_tableViewWithFrame{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_View_Height)];
    self.tableView = tableView;
    self.tableView.backgroundColor = BackgroundColor;
    return self.tableView;
}

- (NSDictionary *)sendParameterWithGet{
    self.responseArr = nil;
    LZYWeakSelf(self);
    self.netWorkBlock = ^(id responseDic) {
        weakself.responseArr = (NSArray *)responseDic;
        [weakself.tableView reloadData];
    };
    NSDictionary *dic = @{@"parameterDic":self.parameterDic,@"url":self.url};
    return dic;
}

- (NSMutableArray *)selectedIdList{
    if (!_selectedIdList) {
        _selectedIdList = [NSMutableArray arrayWithCapacity:self.responseArr.count];
    }
    return _selectedIdList;
}

- (NSMutableArray *)selectedNameList{
    if (!_selectedNameList) {
        _selectedNameList = [NSMutableArray arrayWithCapacity:self.responseArr.count];
    }
    return _selectedNameList;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.tableView showBlankPageViewWithCount:self.responseArr];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SelectSchoolTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SelectSchoolTableViewCell"];
    if (cell == nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"SelectSchoolTableViewCell" owner:self options:nil][0];
        cell.selectionStyle = UIAccessibilityTraitNone;
    }
    if (self.LzySelectType == LZYSelectAloneChoice) {
        if (![NSString isEmpty:self.responseArr[indexPath.row][@"ename"]]) {
            cell.titleNameLabel.text = self.responseArr[indexPath.row][@"ename"];
        }else{
            cell.titleNameLabel.text = self.responseArr[indexPath.row][@"name"];
        }
    }else{
        cell.titleNameLabel.text = self.responseArr[indexPath.row][@"name"];
    }
    if (self.selectedNameList.count != 0 && [self.selectedNameList containsObject: cell.titleNameLabel.text]) {
        cell.multipleSelectImgView.hidden = NO;
    }else{
        cell.multipleSelectImgView.hidden = YES;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.LzySelectType == LZYSelectAloneChoice) {
        if (![NSString isEmpty:self.responseArr[indexPath.row][@"ename"]]) {
            self.LzySelectAlone(self.responseArr[indexPath.row][@"ename"],[NSString stringWithFormat:@"%@",self.responseArr[indexPath.row][@"id"]]);
        }else{
            self.LzySelectAlone(self.responseArr[indexPath.row][@"name"],[NSString stringWithFormat:@"%@",self.responseArr[indexPath.row][@"id"]]);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        if (self.selectedNameList.count == 0) {
            [self.selectedIdList addObject:[NSString stringWithFormat:@"%@",self.responseArr[indexPath.row][@"id"]]];
            [self.selectedNameList addObject:[NSString stringWithFormat:@"%@",self.responseArr[indexPath.row][@"name"]]];
        }else{
            if ([self.selectedNameList containsObject:[NSString stringWithFormat:@"%@",self.responseArr[indexPath.row][@"name"]]]) {
                [self.selectedNameList removeObject:[NSString stringWithFormat:@"%@",self.responseArr[indexPath.row][@"name"]]];
                [self.selectedIdList removeObject:[NSString stringWithFormat:@"%@",self.responseArr[indexPath.row][@"id"]]];
            }else{
                [self.selectedNameList addObject:[NSString stringWithFormat:@"%@",self.responseArr[indexPath.row][@"name"]]];
                [self.selectedIdList addObject:[NSString stringWithFormat:@"%@",self.responseArr[indexPath.row][@"id"]]];
            }
        }
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
